<script setup lang="ts">
import { useColumns } from "./columns";
import { TableProBar } from "@/components/ReTable";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import DictDialog, { initDictDialog } from "./component/dictDialog.vue";
import DictSelector from "@/components/ReDict/src/dictSelector";

defineOptions({
  name: "Dict",
});

const {
  columns,
  form,
  dataList,
  loading,
  formRef,
  tableRef,
  handleDelete,
  reloadChildren,
  handleSelectionChange,
  onSearch,
  loadChildren,
  resetForm,
} = useColumns();

const { openEdit, openAdd } = initDictDialog();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="bg-bg_color w-[99/100] pl-8 pt-4"
    >
      <el-form-item label="字典名称：" prop="label">
        <el-input v-model="form.label" placeholder="请输入字典名称" clearable />
      </el-form-item>
      <el-form-item label="状态：" prop="state">
        <DictSelector
          code="STATE"
          v-model="form.state"
          placeholder="请选择状态"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ep:search')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button
          :icon="useRenderIcon('ep:refresh')"
          @click="resetForm(formRef)"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <TableProBar
      title="字典列表"
      :loading="loading"
      :tableRef="tableRef"
      :dataList="dataList"
      @refresh="onSearch"
    >
      <template #buttons>
        <el-button
          type="primary"
          @click="openAdd"
          :icon="useRenderIcon('ri:add-circle-line')"
        >
          新增字典
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <pure-table
          ref="tableRef"
          border
          align-whole="center"
          row-key="id"
          node-key="id"
          showOverflowTooltip
          table-layout="auto"
          :size="size"
          :data="dataList"
          :columns="columns"
          :checkList="checkList"
          lazy
          :load="loadChildren"
          :tree-props="{ children: 'children', hasChildren: 'isLeaf' }"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)',
          }"
          @selection-change="handleSelectionChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              @click="openEdit(row)"
              :icon="useRenderIcon('ep:edit-pen')"
              :disabled="!row.editable"
            >
              修改
            </el-button>
            <el-popconfirm title="是否确认删除?" @confirm="handleDelete(row)">
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon('ep:delete')"
                  :disabled="!row.editable"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </TableProBar>
    <DictDialog @reload="reloadChildren" />
  </div>
</template>
